#!/bin/bash

function red(){
    echo -e "\033[0;31;1m$1\033[0m"
}

function green(){
    echo -e "\033[0;32;1m$1\033[0m"
}

if [ $# -lt 2 ];then
    green "usage 1: ${PROG_NAME} start|stop|restart|restart-k|status svc_service_run systemd_service"
    green "usage 2: ${PROG_NAME} deploy|undeploy /opt/tiger/xxx/svc_run /opt/tiger/xxx/systemd_service.service"
    red "It doest not support deploy|undeploy using svc now."
    exit 1
fi

opmode=$1
svc_service="$2"
systemd_service=$3

function do_svc(){
    local cmd=$1
    local SVC_RUN_DIR="$2"
    if [ x"$cmd" == x"start" ];then
        SVC_OPT="svc -u"
    elif [ x"$cmd" == x"restart" ];then
        SVC_OPT="svc -h"
    elif [ x"$cmd" == x"reload" ];then
        SVC_OPT="svc -h"
    elif [ x"$cmd" == x"restart-k" ];then
        SVC_OPT="svc -k"
    elif [ x"$cmd" == x"stop" ];then
        SVC_OPT="svc -d"
    elif [ x"$cmd" == x"status" ];then
        SVC_OPT="svstat"
    elif [ x"$cmd" == x"is-active" ];then
        cmd="svstat ${SVC_RUN_DIR}|grep -o 'pid [0-9]*'|sed 's/pid //g'"
        pid1=`bash -c "${cmd}"`
        sleep 5
        pid2=`bash -c "${cmd}"`
        if [ "x$pid1" == "x$pid2" ];then
            echo "active"
        else
            echo "inactive"
        fi
        exit 0
    else
        red "Svc, $cmd does not exist."
        exit -1
    fi
	if [[ $SVC_RUN_DIR =~ /etc/service ]]
	then
		SVC_OPT="sudo $SVC_OPT"
	fi
    cmd="svstat ${SVC_RUN_DIR}|grep -o 'pid [0-9]*'|sed 's/pid //g'"
    before_start=`bash -c "${cmd}"`
    ${SVC_OPT} ${SVC_RUN_DIR}
    if [ x"$cmd" == x"start" ];then
        svc_status
    elif [ x"$cmd" == x"restart" ];then
        svc_status $before_start
    fi
}

function svc_status()
{
  before_start=$1
  cmd="svstat ${SVC_RUN_DIR}|grep -o 'pid [0-9]*'|sed 's/pid //g'"
  after_start=`bash -c "${cmd}"`
  sleep 3
  now_pid=`bash -c "${cmd}"`
  if test -z $now_pid;
  then
      red "${SVC_RUN_DIR} is not started."
      svstat ${SVC_RUN_DIR}
      exit -1
  fi
  echo $now_pid, $before_start, $after_start
  echo x"$now_pid"
  echo x"$before_start"
  if [ ! -z $before_start ]&&[ x"$now_pid" = x"$before_start" ];
  then
      red "${SVC_RUN_DIR} is not restarted."
      svstat ${SVC_RUN_DIR}
      exit -1
  fi
  if [ x"$now_pid" != "$after_start" ];
  then
      red "${SVC_RUN_DIR} is restarted failed."
      svstat ${SVC_RUN_DIR}
      exit -1
  fi
}

function do_systemd()
{
    local cmd=$1
    local SYSTEMD_SERVICE=$2
    if [ x"$cmd" == x"start" ];then
        SYSTEMD_OPT="systemctl --user start"
    elif [ x"$cmd" == x"restart" ];then
        SYSTEMD_OPT="systemctl --user restart"
    elif [ x"$cmd" == x"stop" ];then
        SYSTEMD_OPT="systemctl --user stop"
    elif [ x"$cmd" == x"reload" ];then
        SYSTEMD_OPT="systemctl --user reload"
    elif [ x"$cmd" == x"deploy" ];then
        SYSTEMD_OPT="systemctl --user enable"
    elif [ x"$cmd" == x"undeploy" ];then
        SYSTEMD_OPT="systemctl --user disable"
    elif [ x"$cmd" == x"status" ];then
        SYSTEMD_OPT="systemctl --user status"
    elif [ x"$cmd" == x"is-active" ];then
        sleep 5
        SYSTEMD_OPT="systemctl --user is-active"
    else
        red "Systemctl, $cmd does not exist."
        exit -1
    fi
    before_start=`date +'%s'`
    systemctl --user daemon-reload
    ${SYSTEMD_OPT} ${SYSTEMD_SERVICE}
    if [ x"$cmd" == x"start" ];then
        systemd_status 
    elif [ x"$cmd" == x"restart" ];then
        systemd_status $before_start
    fi
}

function systemd_status()
{
  before_start=$1
  sleep 3
  after_start=`date +'%s'`
  ct=`systemctl --user show -p ExecMainStartTimestamp ${SYSTEMD_SERVICE}|awk -F '=' '{print $2}'`
  st=`date -d "$ct" +%s`
  if test -z $st;
  then
      red "${SYSTEMD_SERVICE} is not started."
      systemctl --user status ${SYSTEMD_SERVICE}
      exit -1
  fi
  if [ ! -z $before_start ]&&[ $st -lt $before_start ];
  then
      red "${SYSTEMD_SERVICE} is not restarted."
      systemctl --user status ${SYSTEMD_SERVICE}
      exit -1
  fi
  if [ $st -ge $after_start ];
  then
      red "${SYSTEMD_SERVICE} is restarted failed."
      systemctl --user status ${SYSTEMD_SERVICE}
      exit -1
  fi
}

function op()
{
    code_name=`lsb_release -c`
    code=$(echo ${code_name#Codename:})
	base_systemd_service=$(basename "$systemd_service")
    # use svc for wheezy
    if [[ x"${code}" == x"wheezy" ]];then
		using=svc
	elif [[ -n "$base_systemd_service" && -e /home/tiger/.config/systemd/user/$base_systemd_service ]]
	then
		using=systemd
	elif [[ $svc_service =~ /.service/ && -e $svc_service ]]
	then
		using=svc
	else
		using=systemd
	fi
	if [[ $using = svc ]]
	then
        do_svc $1 "$2"
	else
        if [ ! -z $3 ];then
            do_systemd $1 $3
        else
            green "SYSTEMD_SERVICE is not defined, skip."
        fi
    fi
}

op ${opmode} "${svc_service}" ${systemd_service}

